<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分布式事务架构指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 30px;
            background: linear-gradient(to bottom, #667eea, #764ba2);
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .timeline-dot {
            position: relative;
        }
        .timeline-dot::after {
            content: '';
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 20px;
            height: 20px;
            background: rgba(102, 126, 234, 0.2);
            border-radius: 50%;
            animation: pulse 2s infinite;
        }
        @keyframes pulse {
            0% {
                transform: translate(-50%, -50%) scale(1);
                opacity: 1;
            }
            100% {
                transform: translate(-50%, -50%) scale(1.5);
                opacity: 0;
            }
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">分布式事务架构指南</h1>
                <p class="text-xl md:text-2xl font-light mb-8 opacity-90">掌握跨服务事务的艺术与科学</p>
                <div class="flex justify-center space-x-8 text-sm">
                    <div class="flex items-center">
                        <i class="fas fa-network-wired mr-2"></i>
                        <span>多服务协同</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-shield-alt mr-2"></i>
                        <span>数据一致性</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-rocket mr-2"></i>
                        <span>高性能方案</span>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction -->
    <section class="py-16 px-6">
        <div class="max-w-4xl mx-auto">
            <div class="bg-white rounded-2xl shadow-lg p-10">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">在</span>分布式系统中，实现分布式事务是一个复杂但非常关键的问题，特别是当多个服务和数据库需要同时参与事务时。分布式事务的目标是保证跨服务的多个操作要么全部成功，要么全部回滚，这在一致性要求很高的场景中至关重要。然而，由于分布式系统的复杂性，网络延迟、节点失败、并发控制等因素都会影响分布式事务的实现。因此，在项目中，设计分布式事务时需要综合考虑性能、可靠性和一致性。
                </p>
            </div>
        </div>
    </section>

    <!-- Challenges Section -->
    <section class="py-16 px-6 bg-gray-100">
        <div class="max-w-6xl mx-auto">
            <h2 class="section-title text-3xl font-bold mb-12 text-gray-800">分布式事务的挑战</h2>
            <div class="grid md:grid-cols-2 lg:grid-cols-4 gap-6">
                <div class="bg-white rounded-xl p-6 card-hover">
                    <div class="text-4xl mb-4 feature-icon">
                        <i class="fas fa-wifi"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">网络不可靠</h3>
                    <p class="text-gray-600">网络延迟、分区、丢包等问题可能导致事务状态不同步。</p>
                </div>
                <div class="bg-white rounded-xl p-6 card-hover">
                    <div class="text-4xl mb-4 feature-icon">
                        <i class="fas fa-server"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">节点失败</h3>
                    <p class="text-gray-600">事务参与方的任意节点可能在操作中失败。</p>
                </div>
                <div class="bg-white rounded-xl p-6 card-hover">
                    <div class="text-4xl mb-4 feature-icon">
                        <i class="fas fa-sync-alt"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">并发控制</h3>
                    <p class="text-gray-600">并发操作对同一资源的修改需要保持一致性。</p>
                </div>
                <div class="bg-white rounded-xl p-6 card-hover">
                    <div class="text-4xl mb-4 feature-icon">
                        <i class="fas fa-balance-scale"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">CAP定理</h3>
                    <p class="text-gray-600">一致性、可用性和分区容忍性无法同时满足，设计时必须权衡。</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Transaction Solutions -->
    <section class="py-16 px-6">
        <div class="max-w-6xl mx-auto">
            <h2 class="section-title text-3xl font-bold mb-12 text-gray-800">常见的分布式事务方案</h2>
            
            <!-- 2PC -->
            <div class="mb-12 bg-white rounded-2xl shadow-lg overflow-hidden">
                <div class="bg-gradient-to-r from-purple-500 to-indigo-600 text-white p-6">
                    <h3 class="text-2xl font-bold flex items-center">
                        <i class="fas fa-handshake mr-3"></i>
                        两阶段提交（2PC）
                    </h3>
                </div>
                <div class="p-8">
                    <p class="text-gray-700 mb-6">2PC 是经典的分布式事务实现机制，主要由协调者和事务参与者构成。流程分为两个阶段：</p>
                    
                    <div class="mermaid">
                        graph LR
                            A[协调者] -->|准备请求| B[参与者1]
                            A -->|准备请求| C[参与者2]
                            B -->|准备就绪| A
                            C -->|准备就绪| A
                            A -->|提交请求| B
                            A -->|提交请求| C
                            B -->|提交完成| D[事务完成]
                            C -->|提交完成| D
                    </div>
                    
                    <div class="grid md:grid-cols-3 gap-6 mt-8">
                        <div class="bg-green-50 rounded-lg p-4">
                            <h4 class="font-semibold text-green-800 mb-2"><i class="fas fa-check-circle mr-2"></i>优点</h4>
                            <p class="text-sm text-gray-700">保证严格的ACID特性，确保一致性。</p>
                        </div>
                        <div class="bg-red-50 rounded-lg p-4">
                            <h4 class="font-semibold text-red-800 mb-2"><i class="fas fa-times-circle mr-2"></i>缺点</h4>
                            <p class="text-sm text-gray-700">性能较低，网络通信和锁资源开销大。协调者宕机可能导致长期不确定状态。</p>
                        </div>
                        <div class="bg-blue-50 rounded-lg p-4">
                            <h4 class="font-semibold text-blue-800 mb-2"><i class="fas fa-lightbulb mr-2"></i>适用场景</h4>
                            <p class="text-sm text-gray-700">需要高一致性要求的场景，如金融系统中跨多个数据库的转账操作。</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 3PC -->
            <div class="mb-12 bg-white rounded-2xl shadow-lg overflow-hidden">
                <div class="bg-gradient-to-r from-indigo-500 to-purple-600 text-white p-6">
                    <h3 class="text-2xl font-bold flex items-center">
                        <i class="fas fa-layer-group mr-3"></i>
                        三阶段提交（3PC）
                    </h3>
                </div>
                <div class="p-8">
                    <p class="text-gray-700 mb-6">3PC 是对2PC的优化，增加了一个"预提交"阶段，以减少协调者宕机时系统的不可用性。</p>
                    
                    <div class="flex justify-center space-x-8 my-8">
                        <div class="text-center">
                            <div class="w-16 h-16 bg-purple-100 rounded-full flex items-center justify-center mb-3 timeline-dot">
                                <i class="fas fa-search text-purple-600 text-xl"></i>
                            </div>
                            <p class="font-semibold">准备阶段</p>
                        </div>
                        <div class="text-center">
                            <div class="w-16 h-16 bg-purple-100 rounded-full flex items-center justify-center mb-3 timeline-dot">
                                <i class="fas fa-clock text-purple-600 text-xl"></i>
                            </div>
                            <p class="font-semibold">预提交阶段</p>
                        </div>
                        <div class="text-center">
                            <div class="w-16 h-16 bg-purple-100 rounded-full flex items-center justify-center mb-3 timeline-dot">
                                <i class="fas fa-check text-purple-600 text-xl"></i>
                            </div>
                            <p class="font-semibold">提交阶段</p>
                        </div>
                    </div>
                    
                    <div class="grid md:grid-cols-3 gap-6">
                        <div class="bg-green-50 rounded-lg p-4">
                            <h4 class="font-semibold text-green-800 mb-2"><i class="fas fa-check-circle mr-2"></i>优点</h4>
                            <p class="text-sm text-gray-700">通过增加阶段，减少宕机带来的不一致性问题。</p>
                        </div>
                        <div class="bg-red-50 rounded-lg p-4">
                            <h4 class="font-semibold text-red-800 mb-2"><i class="fas fa-times-circle mr-2"></i>缺点</h4>
                            <p class="text-sm text-gray-700">比2PC更复杂，性能开销较大。</p>
                        </div>
                        <div class="bg-blue-50 rounded-lg p-4">
                            <h4 class="font-semibold text-blue-800 mb-2"><i class="fas fa-lightbulb mr-2"></i>适用场景</h4>
                            <p class="text-sm text-gray-700">需要高一致性，且系统能够容忍稍微多一些的延迟。</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- TCC -->
            <div class="mb-12 bg-white rounded-2xl shadow-lg overflow-hidden">
                <div class="bg-gradient-to-r from-green-500 to-teal-600 text-white p-6">
                    <h3 class="text-2xl font-bold flex items-center">
                        <i